#ifndef _COMM_HPP_
#define _COMM_HPP_

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>


#endif


#define PATH "."
#define NAME "fifo"


//固定的格式，后面的代码都可以使用
#define ERR_EXIT(error)         \
    do                          \
    {                           \
        perror("com error:");   \
        exit(EXIT_FAILURE);     \
    }                           \
    while(0);


//管道创建于销毁
class NameFifo
{
public:
    // 新建管道
    NameFifo(const std::string &path, const std::string &name) : _path(path), _name(name)
    {
        _fifoname = _path + "/" + _name;
        umask(0);  //设置文件权限
        int n = mkfifo(_fifoname.c_str(),0666); //创建管道
        if(n < 0)
        {
            //报错
            ERR_EXIT("mkfifo");
        }
        else if(n == 0)
        {
            std::cout << "mkfifo success" << std::endl;
        }

    }
    // 删除管道
    ~NameFifo()
    {
        int n = unlink(_fifoname.c_str());   //unlink参数是char*类型 ，_fifoname是string类型所以要.c_str转成c风格的，否则会报错
        if(n < 0)
        {
            ERR_EXIT("mkfifo");
        }
        else if(n == 0)
        {
            std::cout << "unlink success" << std::endl;
        }
    }

private:
    std::string _fifoname;
    std::string _name;
    std::string _path;
};

//管道操作
class FileOper
{
public:
    FileOper(const std::string& path,const std::string& name):
    _path(path),_name(name),_fd(-1)
    {
        _fifoname = path + "/" + name;
        std::cout<<_fifoname;
    }

    //写打开
    void FileForWrite()
    {
        _fd = open(_fifoname.c_str(),O_WRONLY); //写的
        if(_fd < 0)
        {
            ERR_EXIT("open");
        }
        std::cout << "open write success" << std::endl;
    }
    //读打开
    void FileForRead()
    {
        _fd = open(_fifoname.c_str(),O_RDONLY);
        if(_fd < 0)
        {
            ERR_EXIT("open");
        }
        std::cout << "open read success" << std::endl;
    }

    //写操作
    void Write()
    {
        std::string message;
        std::cout << "open write";
        

        while(true)
        {
            std::getline(std::cin,message);  //获取键盘输入
            int len = write(_fd,message.c_str(),message.size());
            if(len < 0)
            {
                ERR_EXIT("write");
            }
            else
            {
                std::cout << message << std::endl;
            }
        }

    }
    //读操作
    void Read()
    {
        char buff[1024];
        std::cout << "oepn read";
        while(true)
        {
            int len = read(_fd,buff,sizeof(buff)-1);
            if(len < 0)
            {
                ERR_EXIT("read");
            }
            else
            {
                buff[len] = 0;
                std::cout << buff << std::endl;
            }
        }
        
    }
    void Close()
    {
        if(_fd > 0)
            close(_fd);
    }

private:
    std::string _fifoname;
    std::string _path;
    std::string _name;
    int _fd;  //文本描述符
    

};



